package com.flyduck.vueshop.common.sotoken.interceptor;

import com.flyduck.vueshop.common.core.constant.CommonConstant;
import com.flyduck.vueshop.common.core.exception.BusinessException;
import com.flyduck.vueshop.common.sotoken.annotation.InnerAuth;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: vueshop-cloud
 * @description: 内部服务Feign接口外部不能访问
 * @author: flyduck
 * @create: 2024-07-07 08:17
 **/
public class InnerAuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        InnerAuth annotation;
        if(handler instanceof HandlerMethod){
            annotation = ((HandlerMethod) handler).getMethodAnnotation(InnerAuth.class);
            if (annotation == null) {
                return true;
            }
            //获取来源
            String source = request.getHeader(CommonConstant.SaSourceConstant.SA_SOURCE_KEY);
            if(!CommonConstant.SaSourceConstant.SA_SOURCE_KEY_INNER.equals(source)){
                throw new BusinessException("外部系统不能访问");
            }
            return true;
        }else {
            return true;
        }
    }
}
